package com.zyd.shiro.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyd.shiro.business.entity.EnterLine;
import com.zyd.shiro.business.service.PakEnterLineService;
import com.zyd.shiro.framework.object.BaseConditionVO;
import com.zyd.shiro.persistence.beans.PakEnterLine;
import com.zyd.shiro.persistence.beans.PakOrderLine;
import com.zyd.shiro.persistence.mapper.PakEnterLineMapper;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class PakEnterLineServiceImpl implements PakEnterLineService {

    @Autowired
    private PakEnterLineMapper pakEnterLineMapper;

    @Override
    public EnterLine insert(EnterLine enterLine) {
        Assert.notNull(enterLine, "对象不可为空！");
        enterLine.getPakEnterLine().setUpdateTime(new Date());
        enterLine.getPakEnterLine().setCreateTime(new Date());
        pakEnterLineMapper.insertSelective(enterLine.getPakEnterLine());
        return enterLine;
    }

    @Override
    public void insertList(List<EnterLine> enterLines) {
            for(EnterLine enterLine:enterLines){
               enterLine.getPakEnterLine().setUpdateTime(new Date());
               enterLine.getPakEnterLine().setCreateTime(new Date());
               pakEnterLineMapper.insertSelective(enterLine.getPakEnterLine());
           }
    }


    /**
     * 根据主键字段进行删除，方法参数必须包含完整的主键属性
     *
     * @param primaryKey
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByPrimaryKey(Long primaryKey) {
        return pakEnterLineMapper.deleteByPrimaryKey(primaryKey) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(EnterLine enterLine) {
        Assert.notNull(enterLine, "对象不可为空！");
        enterLine.setUpdateTime(new Date());
        return pakEnterLineMapper.updateByPrimaryKey(enterLine.getPakEnterLine()) > 0;
    }

    @Override
    public boolean updateSelective(EnterLine enterLine) {
        Assert.notNull(enterLine, "对象不可为空！");
        enterLine.setUpdateTime(new Date());
        return pakEnterLineMapper.updateByPrimaryKeySelective(enterLine.getPakEnterLine()) > 0;
    }

    /**
     * 根据主键字段进行查询，方法参数必须包含完整的主键属性，查询条件使用等号
     *
     * @param primaryKey
     * @return
     */

    @Override
    public EnterLine getByPrimaryKey(Long primaryKey) {
        PakEnterLine pakEnterLine = pakEnterLineMapper.selectByPrimaryKey(primaryKey);
        return null == pakEnterLine ? null : new EnterLine(pakEnterLine);
    }


    /**
     * 根据实体中的属性进行查询，只能有一个返回值，有多个结果时抛出异常，查询条件使用等号
     *
     * @param
     * @return
     */
    @Override
    public EnterLine getOneByEntity(EnterLine test) {
        Assert.notNull(test, "对象不可为空！");
        PakEnterLine pakEnterLine = pakEnterLineMapper.selectOne(test.getPakEnterLine());
        return null == pakEnterLine ? null : new EnterLine(pakEnterLine);
    }

    @Override
    public List<EnterLine> listAll() {
        List<PakEnterLine> pakEnterLines = pakEnterLineMapper.selectAll();
        if (CollectionUtils.isEmpty(pakEnterLines)) {
            return null;
        }
        List<EnterLine> enterLines = new ArrayList<>();
        for (PakEnterLine pakEnterLine : pakEnterLines) {
            enterLines.add(new EnterLine(pakEnterLine));
        }
        return enterLines;
    }

    @Override
    public List<EnterLine> listByEntity(EnterLine entity) {
        return null;
    }


    /**
     * 分页查询
     *
     * @param vo
     * @return
     */
    @Override
    public PageInfo<EnterLine> findPageBreakByCondition(BaseConditionVO vo) {
        PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
        List<PakEnterLine> pakEnterLines = pakEnterLineMapper.findPageBreakByCondition(vo);
        if (CollectionUtils.isEmpty(pakEnterLines)) {
            return null;
        }
        List<EnterLine> enterLines = new ArrayList<>();
        for (PakEnterLine pakEnterLine : pakEnterLines) {
            enterLines.add(new EnterLine(pakEnterLine));
        }
        PageInfo bean = new PageInfo<PakEnterLine>(pakEnterLines);
        bean.setList(enterLines);
        return bean;
    }
    @Override
    public void increase(PakEnterLine pakEnterLine) {
        pakEnterLineMapper.insert(pakEnterLine);
    }

    @Override
    public void increase(PakOrderLine orderLine, Long enterId) {
        PakEnterLine enterLine = new PakEnterLine();
        enterLine.setEnter(enterId);
        enterLine.setMateriel(orderLine.getMateriel());
        enterLine.setNumber(orderLine.getNumber());
        enterLine.setAmt(orderLine.getAmt());
        enterLine.setTotal(orderLine.getTotal());
        increase(enterLine);
    }


}
